Conditions | 7 |
Paths | 14 |
Total Lines | 104 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | 'use strict'; |
||
66 | app.get(/^\/g(?:|rades)$/, co.wrap(function *(req, res) { |
||
67 | // 检查参数 |
||
68 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) { |
||
69 | res.status(404).json({ error: "参数不正确" }); |
||
70 | return; |
||
71 | } |
||
72 | |||
73 | // 记录处理用时 |
||
74 | let start = new Date(); |
||
75 | verbose('Started to query the grades: '.cyan + req.query.id.yellow); |
||
76 | |||
77 | // 登录,获取headers |
||
78 | let headers; |
||
79 | try { |
||
80 | headers = yield access.login(req.query.id, req.query.pwd); |
||
81 | } catch (errMsg) { |
||
82 | logging(errMsg.inner.red); |
||
83 | res.status(404).json({ error: errMsg.public }); |
||
84 | return; |
||
85 | } |
||
86 | verbose('Successfully logged in.'.green); |
||
87 | |||
88 | // 进入成绩页面 |
||
89 | let ires; |
||
90 | try { |
||
91 | // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期 |
||
92 | ires = yield superagent |
||
93 | .post('http://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list') |
||
94 | .set(headers) |
||
95 | .type('form') |
||
96 | .send({ xnxq01id: req.query.sem }) |
||
97 | .endThunk(); |
||
98 | } catch (err) { |
||
99 | logging(`Failed to get grades page\n${err.stack}`.red); |
||
100 | res.status(404).json({ error: '无法进入成绩页面' }); |
||
101 | return; |
||
102 | } finally { |
||
103 | // 异步登出 |
||
104 | co(function *() { |
||
105 | try { |
||
106 | yield access.logout(headers); |
||
107 | } catch (errMsg) { |
||
108 | logging(errMsg.inner.red); |
||
109 | return; |
||
110 | } |
||
111 | verbose('Successfully logged out: '.green + req.query.id.yellow); |
||
112 | }); |
||
113 | } |
||
114 | verbose('Successfully entered grades page.'.green); |
||
115 | |||
116 | let $ = cheerio.load(ires.text); |
||
117 | |||
118 | let top = $('#Top1_divLoginName').text(); |
||
119 | let result = { |
||
120 | name: top.match(/\s.+\(/)[0].replace(/\s|\(/g, ''), |
||
121 | id: top.match(/\(.+\)/)[0].replace(/\(|\)/g, ''), |
||
122 | grades: {}, |
||
123 | 'subject-count': 0, |
||
124 | failed: {}, |
||
125 | 'failed-count': 0, |
||
126 | }; |
||
127 | // 获取成绩列表 |
||
128 | $('#dataList tr').each(function (index) { |
||
129 | if (index === 0) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | let element = $(this).find('td'); |
||
134 | |||
135 | let title = element.eq(3).text().match(/].+$/)[0].substring(1); |
||
136 | let item = { |
||
137 | sem: element.eq(2).text(), |
||
138 | reg: element.eq(4).text(), |
||
139 | exam: element.eq(5).text(), |
||
140 | overall: element.eq(6).text() |
||
141 | }; |
||
142 | if (req.query.details) { |
||
143 | item.id = element.eq(3).text().match(/\[.+\]/)[0].replace(/\[|\]/g, ''); |
||
144 | item.attr = element.eq(8).text(); |
||
145 | item.genre = element.eq(9).text(); |
||
146 | item.credit = element.eq(7).text(); |
||
147 | } |
||
148 | |||
149 | // 如果有补考记录,则以最高分的为准(暂不考虑NaN) |
||
150 | if (title in result.grades && item.overall < result.grades[title].overall) { |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | result.grades[title] = item; |
||
155 | |||
156 | // 挂科判定 |
||
157 | if (element.eq(6).css('color')) { |
||
158 | result.failed[title] = item; |
||
159 | } else { |
||
160 | delete result.failed[title]; |
||
161 | } |
||
162 | }); |
||
163 | |||
164 | result['subject-count'] = Object.keys(result.grades).length; |
||
165 | result['failed-count'] = Object.keys(result.failed).length; |
||
166 | |||
167 | res.json(result); |
||
168 | verbose(`Successfully responded. (req -> res processed in ${new Date() - start}ms)`.green); |
||
169 | })); |
||
170 | |||
255 |